<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
class Reward extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index($type = 'week'){
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $type = $this->request->post('type','');
            $sort = $this->request->post('sort','');
            $start = $this->request->post('start','');
            $end =  $this->request->post('end','');
            $where['status'] = 'normal';
            if($type){
                $where['type'] = $type;
            }
            if($sort){
                switch ($sort) {
                    case 'latest':
                        $orderBy['createtime'] = 'desc';
                        break;
                    case 'likes':
                        $orderBy['likes'] = 'desc';
                        break;
                    case 'comments':
                        $orderBy['comments'] = 'desc';
                        break;
                    case 'flags':
                        $orderBy['flags'] = 'desc';
                        break;
                    case 'latest_flags':
                        $orderBy['new_flag'] = 'desc';
                        break;
                    default:
                        $orderBy['createtime'] = 'desc';
                        break;
                }
            }
            if($start && $end){
             $where['createtime'] = ['between',[strtotime($start),strtotime($end." 23:59:59")]];
            }
            $list = Db::name('Wqdocument')->where($where)->page($page,10)->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        }
        $this->assign('type',$type);
        $nav = Db::name('Category')->where('pid',16)->where('status','normal')->field('id,icon,url,name')->select();
        foreach ($nav as $key => &$value) {
            if($value['id'] == 17){
                $value['postCount'] = Db::name('Document')->where('status','normal')->where('is_shang',1)->count();
            }
            if($value['id'] == 18){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','week')->count();
            }
            if($value['id'] == 19){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','days')->count();
            }
            if($value['id'] == 20){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','fa')->count();
            }
        }
        if($type == 'week'){
            $this->view->assign('typename','闻钱周报');
        }elseif($type == 'days'){
            $this->view->assign('typename','会员日');
        }elseif($type == 'fa'){
            $this->view->assign('typename','闻钱收藏夹');
        }
        $this->view->assign('zhinan',Db::name('Category')->where('id',21)->find());
        $this->view->assign('navs',$nav);
        return $this->view->fetch();
    }
    public function detail(){
        if($this->request->isAjax()){
            $id = $this->request->post('id',0);
            $where['id'] = $id;
            Db::name('wqdocument')->where($where)->setInc('clicks');
            
            $data =Db::name('wqdocument')->where($where)->find();
            Db::name('Document')->where('doc_id',$id)->where('type',$data['type'])->setInc('clicks');
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
                       
            $data['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
            ->where('d.type',$data['type'])->where('d.doc_id',$data['id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
            $data['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
            ->where('d.type',$data['type'])->where('d.doc_id',$data['id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->select();
            if($this->auth->id && $this->auth->level > 0){

            }else{
                $data['content'] = mb_substr(strip_tags($data['content']),0,200).'...';
            }
            return json(['code'=>200,'data'=>$data,'msg'=>'success']);
        
        }
        $nav = Db::name('Category')->where('pid',16)->where('status','normal')->field('id,icon,url,name')->select();
        foreach ($nav as $key => &$value) {
            if($value['id'] == 17){
                $value['postCount'] = Db::name('Document')->where('status','normal')->where('is_shang',1)->count();
            }
            if($value['id'] == 18){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','week')->count();
            }
            if($value['id'] == 19){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','days')->count();
            }
            if($value['id'] == 20){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','fa')->count();
            }
        }
        
        $this->view->assign('zhinan',Db::name('Category')->where('id',21)->find());
        $this->view->assign('navs',$nav);
        $id = $this->request->param('id');
        $this->assign('aid',$id);
        return $this->view->fetch();
    }
}